package cn.harry.modular.sys.service.impl;

import cn.harry.common.utils.SecurityUtils;
import cn.harry.modular.sys.mapper.SysUserRoleMapper;
import cn.harry.modular.sys.domain.SysUserRole;
import cn.harry.modular.sys.service.SysUserRoleService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台用户和角色关系表
 *
 * @author honghh Date 2019-10-12 10:55:44 Copyright (C) www.tech-harry.cn
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole>
		implements SysUserRoleService {

	@Override
	public List<Long> listRoleIdByUserId(Long userId) {
		List<Long> ids = new ArrayList<>();
		List<SysUserRole> sysUserRoles = this.baseMapper.selectList(
				new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
		if (CollectionUtil.isNotEmpty(sysUserRoles)) {
			sysUserRoles.forEach(item -> ids.add(item.getRoleId()));
			return ids;
		}
		return null;
	}

	@Override
	public void insertUserAndUserRole(Long userId, List<Long> roleIdList) {
		this.baseMapper.insertUserAndUserRole(userId, roleIdList);
	}

	@Override
	public void delAndCreateRole(Long userId, List<Long> roleIdList) {
		remove(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
		if (CollUtil.isNotEmpty(roleIdList)) {
			// 保存用户与角色关系
			insertUserAndUserRole(userId, roleIdList);
		}
	}

	@Override
	public List<Integer> listDataScopesByUserId(Long userId) {
		return this.baseMapper.listDataScopesByUserId(userId);
	}

	@Override
	public List<String> listRoleKeyByUserId(Long userId) {
		List<String> roles = new ArrayList<>();
		if (SecurityUtils.isAdmin(userId)) {
			roles.add("admin");
		}
		else {
			roles.addAll(this.baseMapper.listRoleKeyByUserId(userId));
		}
		return roles;
	}

}